package lbframework.bean.passwordField;

import java.awt.Color;
import lbframework.bean.field.LBPlainDocument;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.undo.UndoManager;
import lbframework.classe.Util;

public class LBPasswordField extends javax.swing.JPasswordField {

    public final static String MASK_TEXTO = "Texto";
    public final static String MASK_NUMERO = "Numero";
    private String mascara = "";
    private int columns = 0;
    private boolean caixaAlta = true;
    private boolean obrigatorio = false;
    private LBPlainDocument plain = new LBPlainDocument();
    private UndoManager undo = new UndoManager();

    public LBPasswordField() {
        initComponents();

        this.mascara = MASK_TEXTO;
        setDocument(plain);

        HashSet kTab = new HashSet(1);
        kTab.add(KeyStroke.getKeyStroke("TAB"));
        kTab.add(KeyStroke.getKeyStroke("ENTER"));

        setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, kTab);

        setDisabledTextColor(Color.BLACK);
    }

    @Override
    public int getColumns() {
        return columns;
    }

    public int getInt() {
        if (this.getText().equals("")) {
            return 0;
        } else {
            return Integer.parseInt(this.getText());
        }
    }

    @Override
    public void setColumns(int i_columns) {
        this.columns = i_columns;
        plain.setColumns(i_columns);
    }

    public void setCaixaAlta(boolean i_caixaAlta) {
        caixaAlta = i_caixaAlta;
        plain.setCaixaAlta(i_caixaAlta);
    }

    public boolean isCaixaAlta() {
        return caixaAlta;
    }

    public String getMascara() {
        return mascara;
    }

    public void setMascara(String i_mascara) {
        try {
            mascara = i_mascara;

            if (i_mascara.equals(MASK_TEXTO)) {
                plain.setAcceptedChars(LBPlainDocument.ALPHA_NUMERIC);

            } else if (i_mascara.equals(MASK_NUMERO)) {
                plain.setAcceptedChars(LBPlainDocument.NUMERIC);

            } else {
                plain.setAcceptedChars("");
            }

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }

    public void setInt(int i_int) {
        this.setText(String.valueOf(i_int));
    }

    @Override
    public String getText() {
        return String.valueOf(super.getPassword());
    }

    public boolean isObrigatorio() {
        return obrigatorio;
    }

    public void setObrigatorio(boolean obrigatorio) {
        this.obrigatorio = obrigatorio;

        if (this.isEnabled()) {
            if (obrigatorio) {
                this.setBackground(Util.COR_OBRIGATORIO);
            } else {
                this.setBackground(UIManager.getColor("TextField.background"));
            }
        } else {
            this.setBackground(UIManager.getColor("TextField.disabledBackground"));
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);

        setObrigatorio(obrigatorio);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });
    }// </editor-fold>//GEN-END:initComponents

    private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
        if (isEditable()) {
            Runnable doSelect = new Runnable() {

                public void run() {
                    selectAll();
                }
            };

            SwingUtilities.invokeLater(doSelect);
        }
    }//GEN-LAST:event_formFocusGained

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_Z) {
            if (undo.canUndo()) {
                undo.undo();
            }
        } else if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_Y) {
            if (undo.canRedo()) {
                undo.redo();
            }
        }
    }//GEN-LAST:event_formKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
